// pages/userTags/userTags.js
const API = require('../../common/constant');
const util = require('../../utils/util');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tags: [],
    top: [20, 60, 180, 150, 30],
    left: [20, 240, 60, 500, 450],
    tagsExample: [
      {
        text: "月光族",
        top: "10",
        left: "85"
      },
      {
        text: "有为青年",
        top: "0",
        left: "472"
      },
      {
        text: "90后",
        top: "86",
        left: "289"
      },
      {
        text: "知足常乐",
        top: "130",
        left: "550"
      },
      {
        text: "乐观",
        top: "200",
        left: "94"
      },
      {
        text: "大叔",
        top: "242",
        left: "458"
      },
      {
        text: "萝莉",
        top: "324",
        left: "198"
      }
    ],
    tagInput: '',
    deleteStatus: false,
    label: ''
  },
  /**
   * 获取标签
   */
  getTags: function (tagstr) {
    console.log(tagstr);
    if (tagstr == 'undefined') {
      return;
    } else {
      let tagArr = tagstr.split(';');
      if (tagArr[0].length == 0) {
        console.log('kkkkk');
      } else {
        console.log(tagArr);
        let newData = [];
        for (let i = 0; i < tagArr.length; i++) {
          newData.push({
            text: tagArr[i],
            top: this.data.top[i],
            left: this.data.left[i]
          });
        }
        this.setData({
          tags: newData,
          label: tagstr
        });
        console.log(this.data.tags);
      }
    }


  },
  /**
   * 保存标签
   */
  zczc: function () {
    let label = this.data.label;
    console.log(label);
    util.http({
      url: API.URL.USER.EDIT_MINIUSERINFO,
      data: {
        label: label
      },
      success: res => {
        console.log(res);
        app.showUserInfo('成功修改标签', 'success', 1500);
        setTimeout(() => {
          app.globalData.isNewMyInfo = true
          wx.navigateBack()
        })
      },
      fail: err => {
        console.log(err);
      }
    })
  },
  /**
   * 添加新的标签
   */
  addonetag: function (text) {
    if (this.data.tags.length >= 5) {
      app.showUserInfo('最多添加5个标签', 'none', 1500);
    } else if (this.data.label.indexOf(text) != -1) {
      app.showUserInfo('该标签已经存在', 'none', 1500);
    } else {

      let index = this.data.tags.length;
      let top = this.data.top[index];
      let left = this.data.left[index];
      let newData = this.data.tags;
      let newlabel;
      if (this.data.label.length == 0) {
        console.log('0000');
        newlabel = text;
        console.log(newlabel);
      } else {
        newlabel = this.data.label + ';' + text
      }
      newData.push({
        text: text,
        top: top,
        left: left
      });
      this.setData({
        tags: newData,
        tagInput: '',
        label: newlabel
      });
    }

  },
  /**
   * 获取输入
   */
  addTag: function (e) {
    let text = e.detail.value.tag;

    console.log(e);
    if (e.detail.value.tag.length == 0) {
      app.showUserInfo('请输入标签内容', 'none', 1500);
    } else {
      this.addonetag(text);
    }
  },
  /**
   * 删除tag
   */
  deleteTag: function (e) {

    this.setData({
      deleteStatus: true
    })
  },
  /**
   * 隐藏叉叉
   */
  hideDelete: function (e) {
    this.setData({
      deleteStatus: false
    })
  },
  /**
   * 删除标签
   */
  deletetag: function (e) {
    let newData = this.data.tags;
    newData.splice(e.target.id, 1);
    let newlabel = this.data.label.split(';');
    newlabel.splice(e.target.id, 1);
    let str = newlabel.join(';');
    console.log(str);
    for (let i = 0; i < newData.length; i++) {
      newData[i].top = this.data.top[i];
      newData[i].left = this.data.left[i];
    }
    this.setData({
      tags: newData,
      label: str
    });
  },
  /**
   * 添加上去
   */
  addtagsup: function (e) {
    console.log(e);
    let id = e.target.id;
    console.log(id);
    let text = this.data.tagsExample[id].text;
    console.log(text);
    this.addonetag(text);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    this.getTags(options.tags);
  }
})